#include "DM.h"
#include <cassert>
using namespace nsLibCell;

bool LibCellGrp_C::addLibCell(LibCell_C* pLibCell) {

    if (pLibCell->getFootprint() == _footprint) {
        _vLibCell.push_back(pLibCell);
        return true;
    } else
        return false;
}

void LibCellGrp_C::sortLibCells(void) {
    //sort(_vLibCell.begin(),_vLibCell.end(),_cmpPWR);
    sort(_vLibCell.begin(),_vLibCell.end(),_cmpArea);
}

bool LibCellGrp_C::getIdx(const string& LibCellName, uint32_t& idx) {
    for (uint32_t i=0;i<_vLibCell.size();i++) {
        if (LibCellName == _vLibCell[i]->getName()) {
            idx = i;
            return true;
        }
    }
    return false;
}

LibCell_C* LibCellGrp_C::getLibCell(const uint32_t& idx) {
    if (idx>_vLibCell.size())
        return NULL;
    else
        return _vLibCell[idx];
}

void LibCellGrp_C::showAttr(const uint32_t& idx,const bool& mark) {
    //printf("    GrpName = %s\n",getName().data());

    if (mark)
        printf("   *");
    else
        printf("    ");
    //
    printf("(ref_cell,leakPwr,max cap,delay) = (%s,%.2f,%.2f",
            _vLibCell[idx]->getName().data(),
            _vLibCell[idx]->getLeakagePWR(),
            _vLibCell[idx]->getMaxCap()
            );
}

bool LibCellGrp_C::_cmpPWR(LibCell_C* a, LibCell_C* b) {

    if(a->getLeakagePWR()<b->getLeakagePWR())
        return true;
    else if (a->getLeakagePWR()==b->getLeakagePWR()) {
        vector<string> outPina;
        a->getPins(outPina,e_output);
        assert(outPina.size()==1);
        return a->getMaxCap(outPina[0])<b->getMaxCap(outPina[0]);
    } else
        return false;
}

bool LibCellGrp_C::_cmpArea(LibCell_C* a, LibCell_C* b) {

    if(a->getArea()<b->getArea())
        return true;
    else if (a->getArea()==b->getArea()) {
        if(a->getLeakagePWR()<b->getLeakagePWR()){
            return true;
        } else
            return false;
    } else
        return false;
}

